package com.alstudio.utils.notification;

import com.alstudio.app.LocalEnv;
import com.alstudio.ui.base.BaseActivity;
import com.alstudio.utils.ALActivityManager.ActivityLifeCycleListener;
import com.alstudio.utils.log.Mylog;
import com.alstudio.view.toast.MyToastUtil;

public class NoticeItemShowController implements NoticeItemManagerListener,
		ActivityLifeCycleListener {

	private BaseActivity curActvity;
	private NoticeItem curItem;

	private final String TAG = "NoticeItemShowController";

	private boolean isActivityOn = false;

	private synchronized void showNoticeItem(NoticeItem item) {
		if (isActivityOn == false) {
			Mylog.e("界面已经关闭,稍等下");
			return;
		}

		if (curItem == null) {
			curItem = item;
			Mylog.d("showNoticeItem calling " + curActvity.activityName);
			item.getToast().setContext(curActvity);
			curActvity.onNewNoticeItemAdded(item);
		} else {
			Mylog.d("current showing: " + curItem.getType());
		}
	}

	@Override
	public void onNewNoticeItemAdded(NoticeItem item) {
		// TODO Auto-generated method stub
		Mylog.d("new show notice item requested!");
		showNoticeItem(item);
	}

	@Override
	public void onNoticeItemRemoved(NoticeItem item) {
		// TODO Auto-generated method stub
		Mylog.d("item " + item.getType() + " removed");
		curItem = null;
		curActvity.onNoticeItemRemoved(item);

		NoticeItem nextItem = LocalEnv.mNoticeItemManager.getNextNoticeItem();
		if (nextItem != null) {
			onNewNoticeItemAdded(nextItem);
		}
	}

	@Override
	public void onNoticeItemListEmpty() {
		// TODO Auto-generated method stub
		Mylog.d("onNoticeItemListEmpty calling " + curActvity.activityName);

		curActvity.onNoticeItemListEmpty();
	}

	@Override
	public void onActivityCreate(BaseActivity actvity) {
		// TODO Auto-generated method stub
		Mylog.d(actvity.activityName + " Create");
		if (curItem != null) {
			Mylog.d("dismiss toast in " + curActvity.activityName);
			MyToastUtil.dismissToast();
			curItem = null;
		}
		curActvity = actvity;
	}

	@Override
	public void onActivityStart(BaseActivity actvity) {
		// TODO Auto-generated method stub
		Mylog.d(actvity.activityName + " Start");
	}

	@Override
	public void onActivityResume(BaseActivity actvity) {
		// TODO Auto-generated method stub
		Mylog.e(actvity.activityName + " Resume");
		isActivityOn = true;
		curActvity = actvity;
		NoticeItem item = LocalEnv.mNoticeItemManager.getCurrentNoticeItem();
		if (item != null) {
			Mylog.d(actvity.activityName + " need to show " + item.getType());
			onNewNoticeItemAdded(item);
		}
	}

	@Override
	public void onActivityPause(BaseActivity actvity) {
		// TODO Auto-generated method stub
		Mylog.d(actvity.activityName + " Pause");
		isActivityOn = false;
		if (curItem != null) {
			Mylog.i("dismiss toast in " + curActvity.activityName);
			MyToastUtil.dismissToast();
			curItem = null;
		}
	}

	@Override
	public void onActivityStop(BaseActivity actvity) {
		// TODO Auto-generated method stub
		if (!actvity.equals(curActvity)) {
			Mylog.e("你妹,不是同一个界面停止");
			return;
		}
		Mylog.d(actvity.activityName + " Stop");
		// if(curItem != null)
		// {
		// Mylog.i(TAG,"dismiss toast in "+curActvity.activityName);
		// MyToastUtil.dismissToast();
		// curItem = null;
		// }
	}

	@Override
	public void onActivityFinish(BaseActivity actvity) {
		// TODO Auto-generated method stub
		Mylog.d(actvity.activityName + " Finish");
	}

	@Override
	public void onActivityDestroy(BaseActivity actvity) {
		// TODO Auto-generated method stub
		Mylog.d(actvity.activityName + " Destroy");
	}

}
